/// ITT estimates of bundle choice by bundle discount treatment

********************************************************************************
*** Load data
use "data/pseudodata/userlevel_pseudodata.dta", clear

replace bound = 100 if bound==1

eststo clear

label variable bundle "Bundle Discount"

********************************************************************************
*** Figure: Barchart showing Quantities of Days Purchased by Bundle Discount Treatment

replace num3 = num3 + 1 if paydays1 == 3
replace num7 = num7 + 1 if paydays1 == 7
replace num14 = num14 + 1 if paydays1 == 14 | paydays1 == 12
replace num30 = num30 + 1 if paydays1 == 30 | paydays1 == 24

gen days = 3*num3 + 7*num7 + 14*num14 + 30*num30 if daily==1 & bound==100

foreach i of numlist 3 7 14 30 {
	gen dnum`i' = `i'*num`i'
}

foreach var of varlist num3 num7 num14 num30 {
	gen frac`var' = `var'/payments*100
	gen fracd`var' = d`var'/days*100
}

keep if daily == 1 & bound == 100
keep id fracnum* fracdnum* bundle

reshape long fracnum fracdnum, i(id bundle) j(daysize)
gen lcl = .
gen ucl = .
gen dlcl = .
gen ducl = .
local outcomes = "3 7 14 30"
foreach outcome of local outcomes {
	reg fracnum bundle if daysize==`outcome', r
	local lcl = _b[_cons] + _b[bundle] - invttail(e(df_r), .025) * _se[bundle]
	local ucl = _b[_cons] + _b[bundle] + invttail(e(df_r), .025) * _se[bundle]
	replace lcl = `lcl' if daysize==`outcome' & bundle==1
	replace ucl = `ucl' if daysize==`outcome' & bundle==1
	
	reg fracdnum bundle if daysize==`outcome', r
	local lcl = _b[_cons] + _b[bundle] - invttail(e(df_r), .025) * _se[bundle]
	local ucl = _b[_cons] + _b[bundle] + invttail(e(df_r), .025) * _se[bundle]
	replace dlcl = `lcl' if daysize==`outcome' & bundle==1
	replace ducl = `ucl' if daysize==`outcome' & bundle==1
}

collapse (mean) meanval = fracnum dmeanval = fracdnum, ///
	by(bundle daysize lcl ucl dlcl ducl)
	
gen numtype = .
replace numtype = 1 if daysize == 3
replace numtype = 2 if daysize == 7
replace numtype = 3 if daysize == 14
replace numtype = 4 if daysize == 30

gen dtype = bundle if numtype == 1
replace dtype = bundle + 3 if numtype == 2
replace dtype = bundle + 6 if numtype == 3
replace dtype = bundle + 9 if numtype == 4

/* Note: These counters originally calculated values for purchases (unweighted by days) too. A more parsimonious version of the code would cut them but I leave the index as is to avoid introducing any new errors */
local j=1
qui levelsof dtype
local levs = "`r(levels)'"
foreach i of local levs {
	summ meanval if dtype==`i'
	local m`j' = (round(`r(mean)',.1))
	if `m`j''>10 local m`j' = round(`m`j'',.1)
	local dm`j' = substr("`m`j''", 1,4)
	local j=`j'+1
}
foreach i of local levs {
	summ dmeanval if dtype==`i'
	local m`j' = round(`r(mean)',.1)
	if `m`j''>10 local m`j' = round(`m`j'',.1)
	local dm`j' = substr("`m`j''", 1,4)
	di "`dm`j''"
	local j=`j'+1
}

twoway 	(bar dmeanval dtype if bundle == 0) ///
		(bar dmeanval dtype if bundle == 1, ///
			text(`m9+.5' 0.015 "`dm9'", place(n) size(small)) ///
			text(`m10+.5' 1.015 "`dm10'", place(ne) size(small)) ///
			text(`m11+.5' 3.015 "`dm11'", place(n) size(small)) ///
			text(`m12+.5' 4.015 "`dm12'", place(ne) size(small)) ///
			text(`m13+.5' 6.015 "`dm13'", place(n) size(small)) ///
			text(`m14+.5' 7.015 "`dm14'", place(ne) size(small)) ///
			text(`m15+.5' 9.015 "`dm15'", place(n) size(small)) ///
			text(`m16+.5' 10.015 "`dm16'", place(ne) size(small))) ///
		(rcap dlcl ducl dtype, col("185 217 235")), ylabel(0(20)60) ///
			legend(size(small) ///
				order(1 "No bundle discount" 2 "Bundle discount" 3 "95% CI") pos(2) ring(0)) ///
		xlabel(0.5 "3" 3.5 "7" 6.5 "14" 9.5 "30") ///
		ytitle("") xtitle("Bundle size (days)") ///
		subtitle("Fraction of days purchased", position(11) ring(1) j(left) span)
graph export "output/figures/ITT bundle fracdays.pdf", replace													
									